package com.ld.wms.business.locker.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.wms.business.locker.mapper.LockerExtMapper;
import com.ld.wms.business.locker.mapper.LockerGridMapper;
import com.ld.wms.business.locker.mapper.LockerMapper;
import com.ld.wms.business.locker.model.LockerExtModel;
import com.ld.wms.business.locker.model.LockerGridModel;
import com.ld.wms.business.locker.model.LockerModel;
import com.ld.wms.business.locker.service.LockerService;
import com.ld.wms.business.locker.vo.req.LockerExtReqVo;
import com.ld.wms.business.locker.vo.req.LockerReqVo;
import com.ld.wms.business.locker.vo.resp.LockerExtRespVo;
import com.ld.wms.business.locker.vo.resp.LockerGridRespVo;
import com.ld.wms.business.locker.vo.resp.LockerRespVo;
import com.ld.wms.business.user.model.UserAccountModel;
import com.ld.wms.business.user.service.UserAccountService;
import com.ld.wms.common.vo.GeneratePageListVo;
import com.ld.wms.utils.CommonUtils;
import com.ld.wms.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 智能柜表 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2025-03-28
 */
@Slf4j
@Service
public class LockerServiceImpl extends ServiceImpl<LockerMapper, LockerModel> implements LockerService {

    @Autowired
    private LockerMapper lockerMapper;

    @Autowired
    private JWTUtils jwtUtils;

    @Autowired
    private LockerGridMapper lockerGridMapper;

    @Autowired
    private LockerExtMapper lockerExtMapper;

    @Autowired
    private UserAccountService userAccountService;

    @Override
    public GeneratePageListVo<LockerRespVo> getPageList(Integer current, Integer size) {

        IPage<LockerRespVo> page = new Page<>(current, size);
        IPage<LockerRespVo> lockerRespVoIPage = lockerMapper.selectAllPageList(page);
        Map<Long, UserAccountModel> allUserMap = userAccountService.getAllUserMap();
        for (LockerRespVo respVo : lockerRespVoIPage.getRecords()) {
            UserAccountModel userAccountModel = allUserMap.get(respVo.getCreateBy());
            respVo.setCreateName(userAccountModel.getAccountName());
        }
        return new GeneratePageListVo<>(lockerRespVoIPage);
    }

    @Override
    @Transactional
    public void add(LockerReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        LockerModel lockerModel = new LockerModel();
        BeanUtils.copyProperties(reqVo, lockerModel);
        lockerModel.setCreateBy(currentUserAccountId);
        lockerModel.setCreateTime(new Date());
        lockerMapper.insert(lockerModel);
        // 生成柜体抽屉
        generateLockerGrid(lockerModel.getId(), reqVo.getLockerColumn(), lockerModel.getLockerRow());

    }

    @Override
    public void updateById(LockerReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        LockerModel lockerModel = new LockerModel();
        BeanUtils.copyProperties(reqVo, lockerModel);
        lockerModel.setUpdateBy(currentUserAccountId);
        lockerModel.setUpdateTime(new Date());
        lockerMapper.updateById(lockerModel);

        if (reqVo.getUpdateLockerGridFlag() == 1) {
            // 清空之前的柜体抽屉信息
            Map<String, Object> columnMap = new HashMap<>();
            columnMap.put(LockerGridModel.LOCKER_ID, reqVo.getId());
            lockerGridMapper.deleteByMap(columnMap);
            // 生成柜体抽屉
            generateLockerGrid(lockerModel.getId(), reqVo.getLockerColumn(), lockerModel.getLockerRow());
        }

    }

    @Override
    public LockerRespVo getDetails(Long id) {
        LockerRespVo lockerRespVo = new LockerRespVo();
        LockerModel lockerModel = lockerMapper.selectById(id);
        BeanUtils.copyProperties(lockerModel, lockerRespVo);
        // 查询完整的柜格信息
        List<LockerGridRespVo> lockerGridRespVoList = lockerGridMapper.selectListByLockerId(id);
        lockerRespVo.setLockerGridReqVoList(lockerGridRespVoList);
        return lockerRespVo;
    }

    @Override
    public void delete(Long id) {
        lockerGridMapper.deleteById(id);
        log.info("删除成功，id{}", id);
    }

    @Override
    public LockerExtRespVo getLockerExtByCode(String code) {
        return lockerMapper.selectByCode(code);
    }

    @Override
    public void configLockerExt(LockerExtReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        // 更新柜体滤芯过期时间
        LockerModel lockerModel = new LockerModel();
        lockerModel.setFilterElementExpireTime(reqVo.getFilterElementExpireTime());
        lockerModel.setUpdateTime(new Date());
        lockerModel.setUpdateBy(currentUserAccountId);
        UpdateWrapper<LockerModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(LockerModel.CODE, reqVo.getCode());
        lockerMapper.update(lockerModel, updateWrapper);

        // 更新柜体扩展信息
        LockerExtModel lockerExtModel = new LockerExtModel();
        BeanUtils.copyProperties(reqVo, lockerExtModel);
        UpdateWrapper<LockerExtModel> lockerExtWrapper = new UpdateWrapper<>();
        updateWrapper.eq(LockerModel.CODE, reqVo.getCode());
        lockerExtMapper.update(lockerExtModel, lockerExtWrapper);
    }

    public void generateLockerGrid(Long lockerModelId, Integer lockerColumn, Integer lockerRow) {
        // 生成柜抽屉信息信息
        for (int i = 1;  i <= lockerColumn; i++) {
            String constantSingleABC = CommonUtils.getConstantSingleABC(i - 1);
            for (int j = 1;  j <= lockerRow; j++) {
                LockerGridModel gridModel = new LockerGridModel();
                gridModel.setLockerId(lockerModelId);
                gridModel.setCode(constantSingleABC + j);
                gridModel.setType(0);
                lockerGridMapper.insert(gridModel);
            }
        }

    }
}
